package com.ylyq.ylxx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ylyq.ylxx.constants.AccountConstant;
import com.ylyq.ylxx.context.FilterContextHandler;
import com.ylyq.ylxx.model.bean.GetGoodsListBean;
import com.ylyq.ylxx.model.bean.GetOrderDetail;
import com.ylyq.ylxx.model.bean.GetOrderList;
import com.ylyq.ylxx.model.request.GetGoodsListRequest;
import com.ylyq.ylxx.model.request.GetOrderDetailRequest;
import com.ylyq.ylxx.model.request.GetOrderListRequest;
import com.ylyq.ylxx.service.IJinritemaiService;
import com.ylyq.ylxx.util.HttpClientUtils;
import com.ylyq.ylxx.util.RedisUtil;
import com.ylyq.ylxx.util.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName IJinritemaiServiceImpl
 * @Description TODO
 * @Author xiaweichao
 * @Date 2020/9/17 10:15
 * @Version 1.0
 */
@Service
@Slf4j
public class IJinritemaiServiceImpl implements IJinritemaiService {
    @Autowired
    private RedisUtil redisUtils;

    @Override
    public GetOrderList getOrderList(GetOrderListRequest getOrderListRequest) {
        String access_token = (String) redisUtils.get("Jinritemai_access_token" + FilterContextHandler.getUserID());
        // 请求地址
//        String open_api_url_prefix = "https://openapi-fxg.jinritemai.com/order/list?app_key="+AccountConstant.Jinritemai_APPKey+"&access_token="+access_token+"&method=order.list&param_json={\"end_time\":\"2018/05/31 16:01:02\",\"is_desc\":\"1\",\"page\":\"0\",\"size\":\"10\",\"start_time\":\"2018/04/01 15:03:58\"}&timestamp=2018-06-14%2016:06:59&v=2&sign=your_sign_here";

        String open_api_url_prefix = "https://openapi-fxg.jinritemai.com/order/list?access_token=" + access_token;
        String parms = "&app_key=" + AccountConstant.Jinritemai_APPKey + "&method=order.list&param_json=" + JSONObject.toJSON(getOrderListRequest).toString() + "&timestamp="+ Tools.getCurrentDate("yyyy-MM-dd HH:mm:ss") +"&v=2";
        String sign = "&sign=" + stringToMD5(AccountConstant.Jinritemai_SECRET + parms.replaceAll("=", "").replaceAll("&", "") + AccountConstant.Jinritemai_SECRET);

        String result = HttpClientUtils.get(open_api_url_prefix + parms + sign, "");

        JSONObject jsonObject = JSONObject.parseObject(result);
        GetOrderList getOrderListResult = JSONObject.toJavaObject(jsonObject, GetOrderList.class);
        return getOrderListResult;
    }

    @Override
    public GetOrderDetail getOrderDetail(GetOrderDetailRequest getOrderDetailRequest) {

        String access_token = (String) redisUtils.get("Jinritemai_access_token" + FilterContextHandler.getUserID());
        // 请求地址
//        String open_api_url_prefix = "app_key=your_app_key_here&method=order.detail&param_json={"order_id":"order_id_here"}&timestamp=2018-06-19%2016:06:59&v=2&sign=your_sign_here";

        String open_api_url_prefix = "https://openapi-fxg.jinritemai.com/order/detail?access_token=" + access_token;
        String parms = "&app_key=" + AccountConstant.Jinritemai_APPKey + "&method=order.detail&param_json=" + JSONObject.toJSON(getOrderDetailRequest).toString() + "&timestamp="+ Tools.getCurrentDate("yyyy-MM-dd HH:mm:ss") +"&v=2";
        String sign = "&sign=" + stringToMD5(AccountConstant.Jinritemai_SECRET + parms.replaceAll("=", "").replaceAll("&", "") + AccountConstant.Jinritemai_SECRET);

        String result = HttpClientUtils.post(open_api_url_prefix + parms + sign, "");

        JSONObject jsonObject = JSONObject.parseObject(result);
        GetOrderDetail getOrderDetailResult = JSONObject.toJavaObject(jsonObject, GetOrderDetail.class);
        return getOrderDetailResult;
    }

    @Override
    public GetGoodsListBean getGoodsList(GetGoodsListRequest getOrderDetailRequest) {
        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "tools/advertiser_goods/list/";

        // 构造请求
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());

        String result = HttpClientUtils.get(open_api_url_prefix + uri, JSONObject.toJSON(getOrderDetailRequest).toString(), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        GetGoodsListBean getGoodsListBean = JSONObject.toJavaObject(jsonObject, GetGoodsListBean.class);
        return getGoodsListBean;
    }


    public String stringToMD5(String plainText) {
        byte[] secretBytes = null;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(
                    plainText.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("没有这个md5算法！");
        }
        StringBuilder md5code = new StringBuilder(new BigInteger(1, secretBytes).toString(16));
        while (md5code.length() < 32) {
            md5code.insert(0, "0");
        }
        return md5code.toString();
    }
}
